﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Luna.NHibernate;
using XWord.Fundamental.Data.Repositories;
using XWord.Fundamental.Domain;
using NHibernate;
using NHibernate.Criterion;

namespace XWord.Fundamental.Data.Impl
{
    public class WordRepository : Repository<Word>, IWordRepository
    {

        public WordRepository()  
        {
        }

        public IList<Word> LoadWordList()
        {
            return Session.CreateCriteria<Word>().AddOrder(Order.Asc("IsIgnore")).AddOrder(Order.Asc("Name")).List<Word>();
        }                                                                                        

        public int GetUnKnownWordCount()
        {
            return Convert.ToInt32(Session.CreateQuery
                ("select count(w.Id) from Word as w where w.IsIgnore=false").UniqueResult());
        }

        public void UpdateWord(Word word)
        {
            //word = Session.Get<Word>(word.Id);
            Session.Save(word);
            Session.Flush();
        }

        public void SaveWord(Word word)
        {
            word = Session.Get<Word>(word.Id);
            Session.Save(word);
            Session.Flush();
        }

        public void DeleteWord(Word word)
        {
            Session.Delete(word);
            Session.Flush();
        }

    }
}
